<?php
require("config.php");
if (!file_exists($cfg_arr['storage'])) {
    echo '请先配置storage.db的文件路径';
    echo '<br>';
    echo '例如：E:\storage.db';
    exit('<br>'.$cfg_arr['storage']);
}
if ($_SESSION['user']) {
    header('Location: game.php');
    exit();
}
?>
<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title><?php echo $cfg_arr['title']; ?> - 登陆</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="robots" content="all,follow">
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/toastr.min.css">
    <link rel="stylesheet" href="css/style.default.css" id="theme-stylesheet">
</head>

<body>
    <div class="page login-page">
        <div class="container d-flex align-items-center">
            <div class="form-holder has-shadow">
                <div class="row">
                    <div class="col-lg-6">
                        <div class="info d-flex align-items-center">
                            <div class="content">
                                <div class="logo">
                                    <h1>欢迎登录</h1>
                                </div>
                                <p><?php echo $cfg_arr['title']; ?></p>
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-6 bg-white">
                        <div class="form d-flex align-items-center">
                            <div class="content">
                                <div class="form-group">
                                    <input id="login-username" type="text" name="userName" required data-msg="请输入QQ账号" placeholder="QQ账号" value="" class="input-material">
                                </div>
                                <div class="form-group">
                                    <input id="login-password" type="password" name="passWord" required data-msg="请输入密码" placeholder="游戏密码" class="input-material">
                                    <small>Tips：不是QQ密码</small>
                                </div>
                                <button id="login" onclick="JavaScript:login()" type="submit" class="btn btn-primary">登录</button>
                                <div style="margin-top: -40px;">
                                    <div class="custom-control custom-checkbox " style="float: right;">
                                        <input type="checkbox" class="custom-control-input" id="check1">
                                        <label class="custom-control-label" for="check1">记住密码&nbsp;&nbsp;</label>
                                    </div>
                                </div>
                                <br>
                                <small>没有账号?</small><a href="<?php echo $cfg_arr['url_group']; ?>" class="signup">&nbsp;加入QQ群：<?php echo $cfg_arr['group']; ?></a><br>
                                <small>私聊机器人：设置密码xx，即可设置登陆密码
                                </small>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jquery.validate.min.js"></script>
    <script src="js/md5.min.js"></script>
    <script src="js/toastr.min.js"></script>
    <script>
        toastr.options = {
            closeButton: true,
            positionClass: "toast-bottom-right",
            timeOut: "1000"
        };
        $(function() {
            /*判断上次是否勾选记住密码*/
            var check1s = localStorage.getItem("check1");
            var oldName = localStorage.getItem("userName");
            var oldPass = localStorage.getItem("passWord");
            if (check1s == "true") {
                $("#login-username").val(oldName);
                $("#login-password").val(oldPass);
                $("#check1").prop('checked', true);
            } else {
                $("#login-username").val('');
                $("#login-password").val('');
                $("#check1").prop('checked', false);
            }
        })
        /* 登陆 */
        function login() {
            var v_user = $('#login-username').val();
            var v_pass = $('#login-password').val();
            var i_user = $('#login-username').val().length;
            var i_pass = $('#login-password').val().length;
            if (v_user == "") {
                toastr.error('您还没有填写QQ账号!');
                return false
            }
            if (isNaN(v_user)) {
                toastr.error('请填写正确的QQ账号');
                return false
            }
            if (v_pass == "") {
                toastr.error('您还没有填写密码');
                return false
            }
            if (i_user < 5 || i_user > 11) {
                toastr.error('QQ账号长度请控制在5-11之间');
                return false
            }
            if (i_pass < 6 || i_pass > 16) {
                toastr.error('密码长度请控制在6-16之间');
                return false
            }
            $.post("fun/login.php", {
                username: v_user,
                password: SparkMD5.hash(v_pass),
                time: Date.parse(new Date()) / 1000
            }, function(data, status) {
                var testing = Object.prototype.toString.call(data);
                if (testing !== "[object String]" && testing !== "[object Array]") {
                    toastr.error('连接服务器失败!');
                    return false
                }
                var obj = JSON.parse(data);
                if (obj.error != "0") {
                    toastr.error(obj.msg)
                } else {
                    toastr.success("登录成功");
                    setTimeout(function() {
                        window.location.href = "game.php";
                        window.event.returnValue = false
                    }, 2000)
                }
            })

            var userName = $("#login-username").val();
            var passWord = $("#login-password").val();
            localStorage.setItem("userName", userName)
            localStorage.setItem("passWord", passWord)
            var check1 = $("#check1").prop('checked');
            localStorage.setItem("check1", check1);
        }
    </script>
</body>

</html>